# 自定义中间类（执行是否登录判断）
from django.shortcuts import redirect
from django.urls import reverse
import re


class ShopMiddleware:
    def __init__(self, get_response):
        self.get_response = get_response

    def __call__(self, request):
        path = request.path

        # 定义后台不登录也可直接访问的url列表
        urllist = ['/myadmin/login', '/myadmin/logout', '/myadmin/dologin', '/myadmin/verify']
        if re.match(r'^/myadmin', path) and (path not in urllist):
            if 'adminuser' not in request.session:
                return redirect(reverse("myadmin_login"))

        # 判断点餐请求是否登录
        if re.match(r'^/web', path):
            if 'webuser' not in request.session:
                return redirect(reverse("web_login"))

        # 定义移动端不登录也可直接访问的url列表
        urllist = ['/mobile/register', '/mobile/doregister']
        if re.match(r'^/mobile', path) and (path not in urllist):
            if 'mobileuser' not in request.session:
                return redirect(reverse("mobile_register"))

        response = self.get_response(request)
        return response
